# 购物车Cart
# 购物车属性id
# 方法添加购物车，删除某个商品，清空购物车，购物车商品统计结算
# 商品Prouduct
# 商品属性价格price，id，name，detail
from itertools import product


class Product:
    def __init__(self, id,name, price,detail):
        self.id = id
        self.name = name
        self.price = price
        self.detail = detail

class Cart:
    def __init__(self,id):
        self.id = id
        self.cartList =[]#{product:,count:}

    def addCart(self,product,n):
        for cart in self.cartList:
            if cart["product"].name == product.name:
                cart["count"]+=n
                return {"success":200,"msg":"商品数量添加成功"}
        self.cartList.append({"product": product, "count": n})
        return {"status":200,"msg":"商品添加成功"}
    def delCart(self,product):
        for cart in self.cartList:
            if cart["product"].name == product.name:
                self.cartList.remove(cart)

    def clearCart(self):
        self.cartList.clear()

    def payOrderSave(self):
        totalPrice = 0
        for cart in self.cartList:
            totalPrice += cart["product"].price* cart["count"]
        return {"success":200,"totalPrice":totalPrice,"msg":"结算成功"}

if __name__ == '__main__':
    product1 = Product(1,"水杯",15,"喝水")
    product2 = Product(1, "牛奶", 5, "牛奶加咖啡")
    product3 = Product(1, "面包", 3, "早餐")

    cart = Cart(1)
    print(cart.cartList)

    #添加商品到购物车
    cart.addCart(product1,2)
    cart.addCart(product2,3)
    cart.addCart(product3,4)
    print(cart.cartList)
    cart.addCart(product1,1)
    print(cart.cartList)
    print(cart.payOrderSave())
    cart.delCart(product1)
    print(cart.cartList)
    cart.clearCart()
    print(cart.cartList)

